﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Tenebrous_Manor.SceneManager
{
    class MainEntranceScene
    {
        float elapsed = 0.0f;
        bool initialize;
        bool intro_done;
        bool flashlight;
        bool entrance;

        KeyboardState oldState;
        KeyboardState newState;

        public void Reset()
        {
            elapsed = 0.0f;
            initialize = false;
            intro_done = false;
            flashlight = false;
            entrance = false;
        }

        public bool Update(GameTime gameTime, ref Player player, ref TextBox textBox)
        {
            player.spriteMove(gameTime);

            if (!initialize)
            {
                player.WalkInCutScene(true, Facing.DOWN, GameConstants.COL_COUNT / 2, 5);

                elapsed += (float)gameTime.ElapsedGameTime.Milliseconds;

                if (elapsed >= 3000)
                {
                    elapsed = 0.0f;
                    initialize = true;
                    textBox.setString(Dialogue.dialogue[Dialogue.Dialogue_keys.INTRO_DIALOGUE]);
                    textBox.triggered = true;
                }
            }
            else if (!intro_done)
            {
                if (!textBox.triggered && textBox.done_printing)
                {
                    if (player.getLocation().Y + player.getVelocity() >= (GameConstants.ROW_COUNT - 2) * GameConstants.tileSize.Y)
                        player.setPosition(player.getXIndex(), (GameConstants.ROW_COUNT - 2));

                    if (player.getLocation().Y != (GameConstants.ROW_COUNT - 2) * GameConstants.tileSize.Y)
                    {
                        player.incrementLocation(new Vector2(0, player.getVelocity()));
                    }
                    else
                    {
                        intro_done = true;
                        player.WalkInCutScene(false, Facing.UP, (GameConstants.COL_COUNT / 2), (GameConstants.ROW_COUNT - 2));
                        player.setLight(false);
                        textBox.setString(Dialogue.dialogue[Dialogue.Dialogue_keys.DARK_DIALOGUE]);
                        textBox.triggered = true;
                    }

                }
            }
            else if (!flashlight)
            {
                if (!textBox.triggered && textBox.done_printing)
                {
                    elapsed += gameTime.ElapsedGameTime.Milliseconds;

                    if (elapsed <= 5000f)
                    {
                        if (elapsed >= 2500)
                        {
                            elapsed = 0.0f;
                            flashlight = true;
                            textBox.setString(Dialogue.dialogue[Dialogue.Dialogue_keys.ENTRANCE_DIALOGUE]);
                            textBox.triggered = true;
                        }
                        else if (elapsed >= 1000)
                            player.setLight(true);
                        else if (elapsed >= 500)
                            player.toggleLight();
                    }

                }
            }
            else if (entrance)
            {
                if (!textBox.triggered && textBox.done_printing)
                {
                    entrance = true;
                }
            }
            else
                return true;

            newState = Keyboard.GetState();
            if (textBox.triggered)
            {
                if (newState.IsKeyDown(Keys.Enter) && !oldState.IsKeyDown(Keys.Enter))
                {
                    if (textBox.done_printing)
                        textBox.triggered = !textBox.triggered;

                    else
                        textBox.continued = true;

                }
            }
            textBox.Update(gameTime, ref player.position);
            oldState = newState;

            return false;
        }

        public void Draw(SpriteBatch spriteBatch, SpriteFont font, Texture2D blank, ref Player player, ref TextBox textBox)
        {
            //spriteBatch.Draw(blank, Vector2.Zero, Color.White);
            if(intro_done)
                if(player.getLight())
                    Navigation.RoomGraphics.Draw(spriteBatch, Navigation.Room.MAIN_ENTRANCE);
            player.Draw(spriteBatch);
            textBox.Draw(spriteBatch);
        }
    }
}
